package com.py.credit.controller;

import com.py.credit.common.Page;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.DataCompare;
import com.py.credit.service.DataCompareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dataCompare")
public class DataCompareController {

    @Autowired
    private DataCompareService dataCompareService;

    /**
     * 上传比对模板excel
     *
     * @param jclb    奖惩类别：C:惩戒 J:激励
     * @param file    文件
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doFile.json")
    public RestServiceResult doUploadMultiFile(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                               @RequestParam("file") MultipartFile file,
                                               HttpServletRequest request) {
        RestServiceResult restServiceResult = dataCompareService.doUploadFile(jclb, file);
        request.getSession().setAttribute("temporary", restServiceResult.getData());
        return restServiceResult;
    }

    /**
     * 比对列表
     *
     * @param page    分页条件
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/compareList.json")
    public RestServiceResult doAnalysisMultiFile(Page page, HttpServletRequest request) {
        List<DataCompare> temporary = (List<DataCompare>) request.getSession().getAttribute("temporary");
        int total = temporary.size();
        int firstResult = (page.getPage() - 1) * page.getLimit(), maxResults = page.getLimit();
        List<DataCompare> data = new ArrayList<DataCompare>();
        for (int i = firstResult; i < (firstResult + maxResults) && i < total; i++) {
            data.add(temporary.get(i));
        }

        page.setTotalCount(temporary.size());
        return new RestServiceResult(page.getTotalCount(), data);
    }

    /**
     * 下载比对模板
     *
     * @param jclb     奖惩类别：C:惩戒 J:激励
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/filedownload.json")
    public void downloadFilemb(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb, HttpServletResponse response) {
        dataCompareService.downloadFilemb(jclb, response);
    }

    /**
     * 交叉比对列表
     *
     * @param page 分页条件
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/crossList.json")
    public RestServiceResult getCrossListForPage(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,Page page) {
        List<DataCompare> crossList = dataCompareService.getCrossListForPage(page,jclb);
        return new RestServiceResult(page.getTotalCount(), crossList);
    }

    /**
     * 交叉比对详情列表
     * @param type 主体类型：C:法人 P:自然人
     * @param searchKey 类型为法人传主题名称,类型为自然人传编码
     * @param page 分页条件
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/crossDetailList.json")
    public RestServiceResult getCrossListForPage(@RequestParam(value = "type", defaultValue = "", required = true) String type,
                                                 @RequestParam(value = "searchKey", defaultValue = "", required = true) String searchKey,
                                                 Page page) {
        List<DataCompare> crossDetailList = dataCompareService.selectCrossDetailForPage(page,type,searchKey);
        return new RestServiceResult(page.getTotalCount(), crossDetailList);
    }
}
